
* Appendix Table 11 - Logit regressions health shocks
qui{
clear
use "${data}/MEPS_merged_Health_Whites.dta"
keep if (AGE_YR1<65 & INSCOV_Y1==1) | AGE_YR1>=65 | H1==1 
keep if AGE_YR1<78
drop if H1==. 
drop if R3==.
drop if AGE_YR1==.
gen AGE_CUB_YR1 = AGE_YR1^3

keep if INSCOP_Y1==1  // this covers R1-R3 which we are using here.
keep if year>=2007

rename dp dp1
rename du du1
rename s s1


logit dp1 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1 i.H1  i.R3 i.educ_group  //[pw=Sample_Weight] 
eststo
logit dp1 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1 i.H1 i.R3  // [pw=Sample_Weight] 
eststo
logit dp1 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1 i.H1  // [pw=Sample_Weight] 
eststo
logit du1 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1 i.H1 i.R3 i.educ_group //[pw=Sample_Weight] 
eststo
logit du1 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1 i.H1 i.R3  // [pw=Sample_Weight] 
eststo
logit du1 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1 i.H1  // [pw=Sample_Weight] 
eststo
logit  s1 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1 i.H1 i.R3  i.educ_group // [pw=Sample_Weight] 
eststo
logit  s1 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1 i.H1 i.R3  // [pw=Sample_Weight] 
eststo
logit  s1 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1 i.H1   // [pw=Sample_Weight] 
eststo
	esttab using "${out_tables}/Shocks_initial_values.tex", replace varwidth(25) nogaps   compress label nobaselevels indicate( "Cubic Age = *AGE_YR1* *AGE_SQ_YR1* *AGE_CUB_YR1* " )   ///
	nodepvars nonumbers noconstant mtitles("dp" "dp" "dp" "du" "du" "du" "s" "s" "s" ) star(* 0.1 ** 0.05 *** 0.01) ///
	se   b(3) pr2
	eststo clear
	
	
}
	

* Regressions for shocks - INSURANCE  
**** Tables 9, 10, and 12 appendix   - Health shock regressions and frequencies
qui{
clear
use "${data}/MEPS_merged_Health_Whites.dta"
keep if AGE_YR1<78
drop if H1==. 
drop if R3==.
drop if AGE_YR1==.
gen AGE_CUB_YR1 = AGE_YR1^3
keep if INSCOP_Y1==1  // this covers R1-R3 which we are using here.
rename dp dp1
rename du du1
rename s s1
keep if year>=2007 
drop if INSCOV_Y1==.

gen PRIVATE=1 if INSCOV_Y1==1 
replace PRIVATE=0 if INSCOV_Y1!=1  
label var PRIVATE "PHI"
label define ins6 0 "No PHI" 1 "Has PHI" 
label values PRIVATE  ins6


**** Table 9 appendix
logit dp1 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1 i.H1  i.R3 i.educ_group ib1.PRIVATE 
eststo
logit du1 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1 i.H1 i.R3  i.educ_group ib1.PRIVATE 
eststo
logit  s1 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1 i.H1 i.R3  i.educ_group ib1.PRIVATE  
eststo
logit dp1 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1 i.H1  i.R3 i.educ_group i.INSCOV_Y1  
eststo
logit du1 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1 i.H1 i.R3  i.educ_group i.INSCOV_Y1  
eststo
logit  s1 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1 i.H1 i.R3  i.educ_group i.INSCOV_Y1 
eststo
	esttab using "${out_tables}/Shocks_logits1.tex", varwidth(25) nogaps   compress label nobaselevels indicate( "Cubic Age = *AGE_YR1* *AGE_SQ_YR1* *AGE_CUB_YR1* " ) replace  ///
	nodepvars nonumbers noconstant mtitles("dp"  "du" "s" "dp"  "du" "s") star(* 0.1 ** 0.05 *** 0.01) ///
	se   b(3) 
	eststo clear
	
	
* Table 10 Appendix. Predicted shock probabilities
	
	logit dp1 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1 i.H1  i.R3 i.educ_group i.INSCOV_Y1  
eststo margin: margins i.INSCOV_Y1, at(AGE_YR1=45  AGE_SQ_YR1=2025 AGE_CUB_YR1=91125  ) atmeans post
est sto h1
logit du1 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1 i.H1 i.R3 i.INSCOV_Y1  
eststo margin: margins i.INSCOV_Y1, at(AGE_YR1=45  AGE_SQ_YR1=2025 AGE_CUB_YR1=91125 ) atmeans post
est sto h4
logit  s1 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1 i.H1 i.R3 i.INSCOV_Y1  
eststo margin: margins i.INSCOV_Y1, at(AGE_YR1=45  AGE_SQ_YR1=2025 AGE_CUB_YR1=91125 ) atmeans post
est sto h5
	esttab h1  h4 h5 using "${out_tables}/Shocks_logits_margins_ins.tex", varwidth(25) nogaps   compress label replace  ///
	 nonumbers mtitles("dp"  "du" "s" ) nostar ///
	se   b(3) 
	eststo clear
	
	
	
* Table 12 - left panel - health shock frequencies. 
drop if H1==1
	keep if AGE_YR1>=45 & AGE_YR1<=60	
	table INSCOV_Y1  H1  ,  statistic(mean dp1) statistic(mean du1)  statistic(mean s1)   nformat(%5.2f)  nototal
collect title "Observed health shock frequencies, ages 45-60, excludes Poor H, MEPS"
collect export "${out_tables}/Avg_shock_freq_Mcaid.tex", tableonly replace
	
	
drop if INSCOV_Y1==2 // drop public
	
label var dp1 "dp recorded"
label var du1 "du recorded"
label var s1 "s recorded"
label var INSCOV_Y1 "Health Insurance"

table INSCOV_Y1  H1  if (INSCOV_Y1==1 | INSCOV_Y1==3) ,  statistic(mean dp1) statistic(mean du1)  statistic(mean s1)   nformat(%5.2f)  nototal
collect title "Observed health shock frequencies, ages 45-60, excludes Poor H and those with Public insurance, MEPS"
collect export "${out_tables}/Avg_shock_freq.tex", tableonly replace


}


* probabilities of s shocks 
* Figure 2, s shocks
qui{
clear
use "${data}/MEPS_merged_Health_Whites.dta"
keep if (INSCOV_Y1==1 & AGE_YR1<65) | AGE_YR1>=65 | H1==1
keep if AGE_YR1<78
drop if H1==. 
drop if R3==.
drop if AGE_YR1==.
gen AGE_CUB_YR1 = AGE_YR1^3
keep if INSCOP_Y1==1  // this covers R1-R3 which we are using here.

rename dp dp1
rename du du1
rename s s1


keep if year>=2007 

drop if AGE_YR1==24
* filling it to age 100
	        local new = _N + 23
        set obs `new'
			replace AGE_YR1=78 if AGE_YR1==. & AGE_YR1[_n-1]!=.
						replace AGE_YR1=AGE_YR1[_n-1]+1 if  AGE_SQ_YR1==. & AGE_SQ_YR1[_n-1]==.
    * filling in everything else
	fillin AGE_YR1 H1  R3
		drop if AGE_YR1==. | H1==. 

	replace AGE_SQ_YR1=AGE_YR1^2 
	replace AGE_CUB_YR1=AGE_YR1^3 


logit s1 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1 i.H1 i.R3 
predict y_s

rename AGE_YR1 age
collapse (mean) y_s , by(age H1 R3)
drop if R3==.


* Figure 2, last panel
			#delimit ;
line y_s age if  H1==1 & R3==0, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line y_s age if H1==2 & R3==0, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) lpattern(dash)
|| line y_s age if  H1==3 & R3==0, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) 
 ||, 
  ylabel(0.0(.20)1)
  xlabel(25(5)100)
  ytitle("Fraction")
xtitle("Age")
   title("Frequency of s", color(black))
  legend(label(1 "H=Poor") label(2 "H=Fair")  label(3 "H=Good")   )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr	
graph export "${out_figures}\s_start1.eps", replace

sort age H1 R3
save "${data}/s_start.dta", replace

}


* probabilities of du shocks 
* Figure 2, du shocks
qui{
clear
use "${data}/MEPS_merged_Health_Whites.dta"
keep if (INSCOV_Y1==1 & AGE_YR1<65) | AGE_YR1>=65 | H1==1
keep if AGE_YR1<78
drop if H1==. 
drop if R3==.
drop if AGE_YR1==.
gen AGE_CUB_YR1 = AGE_YR1^3
keep if INSCOP_Y1==1  

rename dp dp1
rename du du1
rename s s1


keep if year>=2007 
drop if AGE_YR1==24


* filling it to age 100
	        local new = _N + 23
        set obs `new'
			replace AGE_YR1=78 if AGE_YR2==. & AGE_YR1[_n-1]!=.
						replace AGE_YR1=AGE_YR1[_n-1]+1 if  AGE_SQ_YR1==. & AGE_SQ_YR1[_n-1]==.
    * filling in everything else
	fillin AGE_YR1 H1  R3
		drop if AGE_YR1==. | H1==. 

	replace AGE_SQ_YR1=AGE_YR1^2 
	replace AGE_CUB_YR1=AGE_YR1^3 

	
logit du1 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1 i.H1 i.R3 
predict y_du

rename AGE_YR1 age
collapse (mean) y_du , by(age H1 R3)
drop if R3==.

			#delimit ;
line y_du age if  H1==1 & R3==0, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line y_du age if H1==2 & R3==0, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) lpattern(dash)
|| line y_du age if  H1==3 & R3==0, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) 
 ||, 
  ylabel(0.0(.20)1)
  xlabel(25(5)100)
  ytitle("Fraction")
xtitle("Age")
   title("Frequency of du", color(black))
  legend(label(1 "H=Poor") label(2 "H=Fair")  label(3 "H=Good")   )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr	
graph export "${out_figures}\du_start.eps", replace

sort age H1 R3
save "${data}/du_start.dta", replace
}

* probabilities of dp shocks 
* Figure 2, dp shocks - top two panels
qui{
clear
use "${data}/MEPS_merged_Health_Whites.dta"
keep if (INSCOV_Y1==1 & AGE_YR1<65) | AGE_YR1>=65  | H1==1
keep if AGE_YR1<78
drop if H1==. 
drop if R3==.
drop if AGE_YR1==.
gen AGE_CUB_YR1 = AGE_YR1^3
keep if INSCOP_Y1==1  

rename dp dp1
rename du du1
rename s s1

keep if year>=2007 
drop if AGE_YR1==24

	* filling it to age 100
	        local new = _N + 23
        set obs `new'
			replace AGE_YR1=78 if AGE_YR1==. & AGE_YR1[_n-1]!=.
						replace AGE_YR1=AGE_YR1[_n-1]+1 if  AGE_SQ_YR1==. & AGE_SQ_YR1[_n-1]==.
    * filling in everything else
	fillin AGE_YR1 H1  R3 educ_group
		drop if AGE_YR1==. | educ_group==. | H1==. | R3==.

	replace AGE_SQ_YR1=AGE_YR1^2 
	replace AGE_CUB_YR1=AGE_YR1^3 
	
logit dp1 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1  i.H1 i.R3 i.educ_group // [pw=Sample_Weight] 
predict y_dp 
	
rename AGE_YR1 age
collapse (mean) y_dp , by(age H1 R3 educ_group)

			#delimit ;
line y_dp age if educ_group==1 & H1==2 & R3==0, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line y_dp age if educ_group==2 & H1==2 & R3==0, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) lpattern(dash)
|| line y_dp age if educ_group==3 & H1==2 & R3==0, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) 
 ||, 
  ylabel(0.0(.20)1)
  xlabel(25(5)100)
  ytitle("Fraction")
xtitle("Age")
   title("Frequency of dp, Fair H, Low R", color(black))
  legend(label(1 "<=HS") label(2 "Some College")  label(3 "College")   )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr	
graph export "${out_figures}\dp_start1.eps", replace


			#delimit ;
line y_dp age if educ_group==1 & H1==1 & R3==0, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line y_dp age if educ_group==1 & H1==2 & R3==0, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) lpattern(dash)
|| line y_dp age if educ_group==1 & H1==3 & R3==0, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) 
 ||, 
  ylabel(0.0(.20)1)
  xlabel(25(5)100)
  ytitle("Fraction")
xtitle("Age")
   title("Frequency of dp, HS or Less, Low R", color(black))
  legend(label(1 "H=Poor") label(2 "H=Fair")  label(3 "H=Good")   )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr	
graph export "${out_figures}\dp_start2.eps", replace
sort age H1 R3
save "${data}/dp_start.dta", replace

}


* combine all shock probabilities into one file and export to folder with parameters for model
qui{
clear
use "${data}/s_start.dta"
merge age H1 R3 using "${data}/du_start.dta"
drop _merge
sort age H1 R3 
merge age H1 R3 using "${data}/dp_start.dta"
sort age H1  educ_group R3
drop _merge

rename y_dp dp1
rename y_du du1
rename y_s s1
gen dp0=1-dp1
gen du0=1-du1
gen s0=1-s1

gen type1= du0*dp0*s0
gen type2= du0*dp1*s0
gen type3= du0*dp0*s1
gen type4= du0*dp1*s1
gen type5= du1*dp0*s0
gen type6= du1*dp1*s0
gen type7= du1*dp0*s1
gen type8= du1*dp1*s1


keep educ_group age H1 R3 type*

reshape long type, i(educ_group age R3 H1) j(A)
keep if age>=25
sort educ_group A H1 R3 age
drop educ_group A H1 R3 age
outsheet using "${data_model}/parameters_health_shocks.txt", nolabel nonames replace
}


